package org.david.code.web.action;




import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.david.code.jdbc.bean.DBTable;
import org.david.code.service.IDataService;
import org.springframework.stereotype.Service;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionSupport;
public class DataAction extends ActionSupport{
	Logger logger = Logger.getLogger(DataAction.class);
	@Resource(name="dataServiceImpl")
	private IDataService iDataService;
	public IDataService getiDataService() {
		if(iDataService==null){
			WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
			this.iDataService = (IDataService)ctx.getBean("dataServiceImpl");
		}
		return iDataService;
	}
	public void setiDataService(IDataService iDataService) {
		this.iDataService = iDataService;
	}
	private Map root; 
	public Map getRoot() {
		return root;
	}
	public void setRoot(Map root) {
		this.root = root;
	}
	public String execute() throws Exception {
		System.out.println("--------------------");
		//logger.error("error......");
		logger.debug("debug......");
		logger.info("info......");
        return SUCCESS;
    }
	public String index() throws Exception {
		String isRefresh=ServletActionContext.getRequest().getParameter("isRefresh");
		logger.info("query all table");
		try {
			List tablelist=this.getiDataService().getAllTable(isRefresh);
			ServletActionContext.getRequest().setAttribute("tablelist", tablelist);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public String getColumnByTable() throws Exception{
		try {
			String tableName=ServletActionContext.getRequest().getParameter("tableName");
			DBTable table = this.getiDataService().getTableColumns(tableName);
			root=new HashMap<String,String>();
			root.put("table", table);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return "json";
	}
	public String pager() throws Exception {
		System.out.println("--------------------");
		//logger.error("error......");
		logger.debug("debug......");
		logger.info("info......");
		return SUCCESS;
	}
    
}
